package com.job2e.common.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class SystemUtil {
    public static HashMap<String, String> getParamMap(String[] args) {
        HashMap<String, String> paramMap = new HashMap<String, String>();
        // 校验输入参数
        for (String arg : args) {
            int index = arg.indexOf('=');
            String name = null;
            String value = null;
            if (index < 0) {
                name = arg;
            } else {
                name = arg.substring(0, index);
                value = arg.substring(index + 1);
            }
            if (null == value) {
                value = "";
            }
            paramMap.put(name, value);
        }
        return paramMap;
    }

    public static boolean isStringEquals(String s1, String s2) {
        if (s1 != null) {
            return s1.equals(s2);
        }
        if (s2 == null) {
            return true;
        }
        return false;

    }

    public static boolean isEmpty(String v) {
        return v == null || v.length() == 0;
    }

    public static String getValue(Map<String, String> map, String key) {
        return getValue(map, key, null);
    }

    public static String getValue(Map<String, String> map, String key, String defaultValue) {
        String value = map.get(key);
        if (value != null) {
            value = value.trim();
        }

        if (isEmpty(value)) {
            return defaultValue;
        }
        return value;
    }

    public static List<String> getTextBlock(Iterator<String> iter, String textBlockSep) {
        List<String> cells = new LinkedList<String>();
        String tempStr;
        while (iter.hasNext()) {
            tempStr = iter.next();
            if (SystemUtil.isStringEquals(tempStr, textBlockSep)
                    || (!SystemUtil.isEmpty(textBlockSep) && tempStr.matches(textBlockSep))) {
                if (cells.size() == 0) {
                    // Block的前导的分隔行，跳过
                    continue;
                }
                break;
            }
            cells.add(tempStr);
        }
        if (!iter.hasNext() && cells.size() == 0) {
            return null;
        }
        return cells;
    }

    public static List<String> executeCmd(String cmd) {
        Process p = null;
        BufferedReader br = null;
        try {
            p = Runtime.getRuntime().exec(cmd);
            br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            List<String> result = new ArrayList<String>();
            String str = null;
            while ((str = br.readLine()) != null) {
                result.add(str);
            }
            try {
                if (p.waitFor() != 0)
                    return null;
            } catch (InterruptedException e) {
                return null;
            }
            return result;
        } catch (IOException e) {
            System.out.println(e);
            return null;
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {}
            }
            if (p != null) {
                p.destroy();

            }
        }
    }

    public static List<String> getAllLocalHostIP() throws SocketException {
        List<String> ips = new ArrayList<String>();
        Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
        InetAddress ip = null;
        while (allNetInterfaces.hasMoreElements()) {
            NetworkInterface netInterface = allNetInterfaces.nextElement();
            Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
            while (addresses.hasMoreElements()) {
                ip = addresses.nextElement();
                if (ip != null && ip instanceof Inet4Address) {
                    ips.add(ip.getHostAddress());
                }
            }
        }
        return ips;
    }

    public static String getLocalHostIP() throws SocketException {
        List<String> allLocalHostIP = getAllLocalHostIP();
        for (String ip : allLocalHostIP) {
            if (!ip.equals("127.0.0.1") && !ip.equals("0.0.0.0")) {
                return ip;
            }
        }
        return null;
    }
}
